package formula.web;

import java.io.Serializable;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;

import twork.jsf.util.TworkJsfMessageUtil;

import com.sun.faces.util.MessageFactory;

import formula.business.dao.DriverDao;
import formula.business.model.Driver;

@SuppressWarnings("serial")
@ManagedBean
@SessionScoped
public class DriverHome implements Serializable {

	public static final String EL_NAME = "#{driverHome}";

	@ManagedProperty(value = DriverDao.EL_NAME)
	private DriverDao driverDao;

	private Driver driver;

	private Long driverId;

	/**
	 * All the logic is HERE, it is easier than a Factory Pattern, and reusable
	 */
	public Driver getDriver() {
		if (driver == null) {
			if ((driverId == null)) {
				driver = new Driver();
			} else if ((driverId != null) && (driver == null)) {
				driver = driverDao.selectDriverById(driverId);
			}
		}
		return driver;
	}

	public void clear() {
		driver = null;
		driverId = null;
	}

	public void doMerge() {
		// the id of driver will be null if we are in creation mode
		Long idBeforeMerge = driver.getId();
		driver = driverDao.merge(driver);
		String key = idBeforeMerge == null ? "success.create" : "success.update";
		TworkJsfMessageUtil.addMessageInfo("appMsg", key, Driver.class.getSimpleName(), driver.getLastname());
		driver = null;
		driverId = null;
	}

	public void doDelete() {
		driverDao.delete(driver);
		//
		TworkJsfMessageUtil.addMessageInfo("appMsg", "success.delete", "Driver", driver.getLastname());
		driver = null;
		driverId = null;
	}

	public void setDriverDao(DriverDao driverDao) {
		this.driverDao = driverDao;
	}

	public Long getDriverId() {
		return driverId;
	}

	public void setDriverId(Long _driverId) {
		if (_driverId != null) {
			driver = null;
			driverId = _driverId;
		}
	}

	public void setDriver(Driver driver) {
		this.driver = driver;
	}

	public boolean isDriverEmpty() {
		if (driver == null) {
			if ((driverId == null)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * added Validator for age
	 */
	public void validateAge(FacesContext f, UIComponent c, Object value) {
		if (value instanceof Integer) {
			Integer age = (Integer) value;
			if (age != null && age > 45) {
				FacesMessage dd;
				String label = MessageFactory.getLabel(f, c).toString();
				FacesMessage fm = TworkJsfMessageUtil.createMessageError("appMsg", "failure.tooOld", label);
				TworkJsfMessageUtil.addMessageInfo("appMsg", "failure.tooOld", label);
				throw new ValidatorException(fm);
			}
			if (age != null && age < 18) {
				String label = MessageFactory.getLabel(f, c).toString();
				FacesMessage fm = TworkJsfMessageUtil.createMessageError("appMsg", "failure.tooYoung", label);
				throw new ValidatorException(fm);
			}
		}
	}
}
